<?php 
class cls_data_favorite extends cls_data_base
{
	function check_if_related_data($customerid, $favoriteid)
	{
		$table_name = '`favorites`';
	
		$sql = "select * from {$table_name} where CustomerID = {$customerid} and ID = {$favoriteid}";
		$result = $this->db->query($sql);
		$rows = $this->db->num_rows($result);
	
		if(!$rows)
		{
			return false;
		}
		elseif ($rows != 1)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	function get_all_favorite($customerid)
	{
		$favorite_table_name = '`favorites`';
		$product_table_name = '`product`';
		$productstatistic_table_name = '`productstatistic`';
	
		//return book name, image, author, language and amount(have or not)
		$sql = "select {$favorite_table_name}.ID, {$favorite_table_name}.ProductID, {$product_table_name}.Name, {$product_table_name}.IMAGE, {$product_table_name}.Author, {$product_table_name}.ProductLanguage, {$productstatistic_table_name}.Quantity from {$favorite_table_name}, {$product_table_name}, {$productstatistic_table_name} where {$product_table_name}.ID = {$favorite_table_name}.ProductID and {$favorite_table_name}.CustomerID = {$customerid} and {$product_table_name}.ID = {$productstatistic_table_name}.ProductID";
		$result = $this->db->getAll($sql);
		
		return $result;
	}
	
	function add_favorite($customerid, $productid)
	{
		$table_name = '`favorites`';
	
		$input = array(
				"CustomerID" => $customerid,
				"ProductID" => $productid);
		$result = $this->db->insert($table_name, $input);
	
		return $result;
	}
	
	function delete_favorite($favoriteid)
	{
		$table_name = '`favorites`';
	
		$result = $this->db->delete($table_name, "ID = {$favoriteid}");
	
		return $result;
	}
	
	function check_favorite_exist($customerid, $productid)
	{
		$table_name = '`favorites`';
	
		$sql = "select * from {$table_name} where CustomerID = {$customerid} and ProductID = {$productid}";
		$result = $this->db->query($sql);
		$matched_record = $this->db->num_rows($result);
		if($matched_record == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
?>